/*
@title: Quest_to_Heaven
@description: Quest_to_Heaven is a puzzle game where you play as an angel trying to escape a dark place by navigating through demons and entities. The player must find keys, unlock locks, and avoid touching demons to progress through the levels.
@author: Alainy R
@tags: ['puzzle']
@addedOn: 2024-05-20
@img: ""
*/
	const player = "p"
    const goal = "e"
    const wall = "w"
    const trap = "l"
    const key = "k"
    const lock = "c"
    const back = "b"
    const entity = "t"
   
    
setLegend(
	[ player, bitmap`
.....66666......
....6.....6.....
.....66666......
.....11111......
....1222221.....
.2..1202021..2..
222.1222221.222.
2222.11111.2222.
222217777712222.
22.21777771.222.
2...1777771...2.
....1777771.....
.....77777......
.....11.11......
.....11.11......
.....11.11......` ],
    [ goal, bitmap`
............3...
...3......3.....
.6...363333..3..
....33339933.3..
...333966993336.
.3333962269363..
...3962222933...
...3922222693...
...39222226936.3
6..39222226933..
...3396226993...
...633926933....
.3...339333.3...
...3...33....3..
........3.......
................`],
    [ wall, bitmap`
9699999963399999
9969933363693333
9336663636633366
3399966633939999
9339693633699999
6636693399933333
3339933633666699
3999933666699366
3333639399939333
9333366639993933
9936333336999336
3666999333966666
6633333333333933
9666339999399999
9999666999339993
9399936666666699`],
    [ trap, bitmap`
...3.......3....
..3.........3...
...33.333.33....
....3333333.....
.....23332......
..3..32323..3...
3333..333..3333.
.3333..3..3333..
.....33333......
....3333333.....
.....33333......
.....33333......
...3..333.L.....
.....L333..3....
.....3.3........
...L..3...3.....`],
    [ key, bitmap`
................
.111............
12621...........
.111............
......1111......
....11122111....
...1122662211...
.11122692622111.
.12222699622221.
.11222666622211.
..111122221111..
.....11111......
............111.
...........12621
............111.
................`],
    [ lock, bitmap`
......33...3....
.3.....3........
....3.333...3...
3...3.3333..3...
...33.333..33..3
...333333.333...
..339339333333..
..339939333333..
.3339996339933..
.3399666996933..
..399666669333..
..33666766933...
...366776693....
...33677763.....
....3933333.....
................`],
    [ back, bitmap`
0000000000000000
0000000000000000
0000L000000000L0
000L000000000000
0000000000000000
0000000000000000
00000000LL000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
000L0000000000L0
00L0000000000000
000000000L000000
0000000000000000
0000000000000000`],
    [ entity, bitmap `
................
................
.111............
12921...........
.111...11.......
.....111111.....
....11333311....
...1123993211...
..112239932211..
.212223333222122
...1112222111...
......1111......
............111.
...........12921
............111.
................`]
  );
setSolids([ player, wall, lock ])

let level = 0; 
const levels = [
  map`
...e...
..www.l
l......
.......
www.www
www.www
www.www
wwwpwww`,
  map`
..l.....tw
.........w
..wwwwwwww
..........
...wpw..l.
wwwwww....
...l......
e......l..
..........`,
  map`
........w
...l....w
..www.w.w
l.wew.w.w
..w.w.w.w
..w.w.w.w
..w.wpw.w
.lw.wwwtw
....wwwww`,
  map`
.........
..wwwwl..
....tw...
.l...w...
..wwww..l
..w......
k.w.wwwwc
www.we...
p...w....`,
  map`
l...w...e
....w....
..l..c...
.ww...ww.
w...p...w
.ww...ww.
......l..
.t..w....
k...w..l.`,
  map`
wpw.ewwwwww
w.wl.......
w..wwwwww.l
w...l..kw..
w.w....www.
wwww.www.w.
w..l.wl..w.
ww......lw.
w..www...w.
wl.....www.
wwwwwwc....`
]
                
setMap(levels[level])
const currentLevel = levels[level]; 
setMap(currentLevel);

setPushables({
	[ player ]: []
})

//Enemy trap

afterInput(() => {
    const trapsCovered = tilesWith(player, trap); 
    
    if (trapsCovered.length > 0) {
        setMap(levels[level])
    }
})



//movement

onInput("s", () => {
	getFirst(player).y += 1;
});

onInput("w", () => {
	getFirst(player).y -= 1;
});

onInput("d", () => {
	getFirst(player).x += 1;
})

onInput("a", () => {
	getFirst(player).x -= 1
})


setBackground(back)

//Success
 afterInput(() => {
    const goalsCovered = tilesWith(player, goal); 
    if (goalsCovered.length >= 1) {
        
        level = level + 1;
        if (level < levels.length) {
            setMap(levels[level]);
        } else {
            addText("YOU ESCAPED", { y: 4, color: color`2` });
        }
    }
});


// Key/lock

afterInput(() => {
  const goalsCovered = tilesWith(player, goal);
  const keysTaken = tilesWith(player, key); 
if (goalsCovered.length >= 1) {
level = level + 1;
    const currentLevel = levels[level];
    if (currentLevel !== undefined) {
      setMap(currentLevel);
    } else {
    }
}
if (keysTaken.length >= 1) {
getFirst(lock).remove();
getFirst(key).remove();
}
});

//Music

const melody = tune`
200: D5~200 + G5-200,
200: G4-200 + B4~200,
200: D4/200 + F4/200 + D5~200,
200: B4~200 + D4^200,
200: D5~200 + G4-200,
200: F4/200 + A4/200 + B4~200 + D4^200,
200: G4/200 + B4/200 + D5~200 + D4^200,
200: B4~200 + G4-200,
200: A4/200 + C5/200 + D5~200 + D4^200,
200: G4/200 + B4/200,
200: F4/200 + A4/200 + D5~200 + G5-200 + D4^200,
200: D4^200 + B4~200,
200: D4/200 + F4/200 + D5~200 + G5-200 + G4^200,
200: C4/200 + E4/200 + G5~200 + B4~200 + D4^200,
200: D4/200 + F4/200 + D5~200 + G5-200 + G4^200,
200: G5~200 + B4~200 + D4^200,
200: F4/200 + D5~200 + G5-200 + D4^200 + G4^200,
200: G5~200 + B4~200,
200: D4/200 + F4/200 + D5~200 + G4-200,
200: B4~200,
200: D5~200 + D4^200,
200: F4/200 + A4/200 + B4~200 + D4^200 + G4-200,
200: G4/200 + B4/200 + D5~200,
200: B4~200 + D4^200,
200: A4/200 + C5/200 + D5~200 + G4-200,
200: G4/200 + B4/200 + D4^200,
200: F4/200 + A4/200 + D5~200 + D4^200 + G5-200,
200: B4~200 + G4-200,
200: D4/200 + F4/200 + D5~200 + G5-200,
200: B4~200,
200: D4/200 + F4/200 + D5~200 + G4-200 + G5-200,
200: B4~200`
playTune(melody) 
const playback = playTune(melody, Infinity)

const enemyLaugh = tune`
62.37006237006237: B5~62.37006237006237,
62.37006237006237: B5~62.37006237006237 + A5~62.37006237006237,
62.37006237006237: B5~62.37006237006237 + A5~62.37006237006237,
62.37006237006237: B5~62.37006237006237 + A5~62.37006237006237,
62.37006237006237: B5~62.37006237006237 + A5~62.37006237006237,
62.37006237006237: F5~62.37006237006237 + E5~62.37006237006237 + A5~62.37006237006237 + G5~62.37006237006237,
62.37006237006237: E5~62.37006237006237 + D5~62.37006237006237,
62.37006237006237: D5~62.37006237006237 + C5~62.37006237006237,
62.37006237006237: C5~62.37006237006237 + B4~62.37006237006237,
62.37006237006237: B4~62.37006237006237 + A4~62.37006237006237 + G4~62.37006237006237,
1372.1413721413721`

